var map, vectors, formats, vector_layer;

function show_alert( geojson)
{
alert(geojson);
}

function updateFormats() {
	var in_options = {
		'internalProjection' : map.baseLayer.projection,
		'externalProjection' : new OpenLayers.Projection("EPSG:4326")
	};
	var out_options = {
		'internalProjection' : map.baseLayer.projection,
		'externalProjection' : new OpenLayers.Projection("EPSG:4326")
	};
	var gmlOptions = {
		featureType : "feature",
		featureNS : "http://example.com/feature"
	};
	var gmlOptionsIn = OpenLayers.Util.extend(OpenLayers.Util.extend({},
			gmlOptions), in_options);
	var gmlOptionsOut = OpenLayers.Util.extend(OpenLayers.Util.extend({},
			gmlOptions), out_options);
	var kmlOptionsIn = OpenLayers.Util.extend({
		extractStyles : true
	}, in_options);
	formats = {
		'in' : {
			wkt : new OpenLayers.Format.WKT(in_options),
			geojson : new OpenLayers.Format.GeoJSON(in_options),
			georss : new OpenLayers.Format.GeoRSS(in_options),
			gml2 : new OpenLayers.Format.GML.v2(gmlOptionsIn),
			gml3 : new OpenLayers.Format.GML.v3(gmlOptionsIn),
			kml : new OpenLayers.Format.KML(kmlOptionsIn),
			atom : new OpenLayers.Format.Atom(in_options),
			gpx : new OpenLayers.Format.GPX(in_options)
		},
		'out' : {
			wkt : new OpenLayers.Format.WKT(out_options),
			geojson : new OpenLayers.Format.GeoJSON(out_options),
			georss : new OpenLayers.Format.GeoRSS(out_options),
			gml2 : new OpenLayers.Format.GML.v2(gmlOptionsOut),
			gml3 : new OpenLayers.Format.GML.v3(gmlOptionsOut),
			kml : new OpenLayers.Format.KML(out_options),
			atom : new OpenLayers.Format.Atom(out_options),
			gpx : new OpenLayers.Format.GPX(out_options)
		}
	};
}

function init() {
	map = new OpenLayers.Map('map');
	var wms = new OpenLayers.Layer.WMS("OpenLayers WMS",
			"http://vmap0.tiles.osgeo.org/wms/vmap0?", {
				layers : 'basic'
			});

	vectors = new OpenLayers.Layer.Vector("Vector Layer");

	map.addLayers([ wms, vectors ]);
	map.addControl(new OpenLayers.Control.MousePosition());
	map.addControl(new OpenLayers.Control.EditingToolbar(vectors));

	var options = {
		hover : true,
		onSelect : serialize
	};
	var select = new OpenLayers.Control.SelectFeature(vectors, options);
	map.addControl(select);
	select.activate();

	updateFormats();
	map.setCenter(new OpenLayers.LonLat(0, 0), 1);
}

function addJson(geojsonStr) {
	if (vector_layer != null)
	{
		map.deleteLayer( vector_layer );
	}
	if ( geojsonStr != null )
	{
		var geojson_format = new OpenLayers.Format.GeoJSON();
	    vector_layer = new OpenLayers.Layer.Vector(); 
	    map.addLayer(vector_layer);	    
	    vector_layer.addFeatures(geojson_format.read(geojsonStr));
	}
    
}

function serialize(feature) {
	var type = document.getElementById("formatType").value;
	// second argument for pretty printing (geojson only)
	var pretty = document.getElementById("prettyPrint").checked;
	var str = formats['out'][type].write(feature, pretty);
	// not a good idea in general, just for this demo
	str = str.replace(/,/g, ', ');
	document.getElementById('output').value = str;
}

function deserialize() {
	var element = document.getElementById('text');
	var type = document.getElementById("formatType").value;
	var features = formats['in'][type].read(element.value);
	var bounds;
	if (features) {
		if (features.constructor != Array) {
			features = [ features ];
		}
		for ( var i = 0; i < features.length; ++i) {
			if (!bounds) {
				bounds = features[i].geometry.getBounds();
			} else {
				bounds.extend(features[i].geometry.getBounds());
			}

		}
		vectors.addFeatures(features);
		map.zoomToExtent(bounds);
		var plural = (features.length > 1) ? 's' : '';
		element.value = features.length + ' feature' + plural + ' added';
	} else {
		element.value = 'Bad input ' + type;
	}
}

// preload images
(function() {
	var roots = [ "draw_point", "draw_line", "draw_polygon", "pan" ];
	var onImages = [];
	var offImages = [];
	for ( var i = 0; i < roots.length; ++i) {
		onImages[i] = new Image();
		onImages[i].src = "../theme/default/img/" + roots[i] + "_on.png";
		offImages[i] = new Image();
		offImages[i].src = "../theme/default/img/" + roots[i] + "_on.png";
	}
})();
